package com.taotao.controller;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.CommonResult;
import com.taotao.common.EasyUIDataGridResult;
import com.taotao.common.PictureResult;
import com.taotao.pojo.TbUser;
import com.taotao.service.PictureService;
import com.taotao.service.UserService;
import com.taotao.utils.ModelUtils;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private PictureService pictureService;

	@RequestMapping("/user/list")
	@ResponseBody
	public EasyUIDataGridResult getUserList(Integer page, Integer rows) {
		return userService.getUserList(page, rows);
	}

	@RequestMapping("/user/{username}")
	@ResponseBody
	public TbUser getUser(@PathVariable("username") String username) {
		return userService.getUserByOtherWay(username);
	}

	@RequestMapping(value = "/user/{id}", method = RequestMethod.PATCH)
	@ResponseBody
	public CommonResult editUser(@PathVariable("id") Long id, @RequestBody Map<String, String> map)
			throws IllegalAccessException, InstantiationException, InvocationTargetException, IntrospectionException {
		// CommonsMultipartResolver commonsMultipartResolver = new
		// CommonsMultipartResolver();
		// MultipartHttpServletRequest multipartRequest = commonsMultipartResolver
		// .resolveMultipart((HttpServletRequest) shiroRequest.getRequest());
		// MultipartFile file = multipartRequest.getFile("file");
		// Map parameterMap = multipartRequest.getParameterMap();
		// 接收base64类型的图片数据
		String base64Data = map.get("base64Data");
		TbUser tbUser = ModelUtils.convertMap(TbUser.class, map);
		PictureResult uploadPicture = pictureService.uploadPictureFromBase64(base64Data);
		String url = uploadPicture.getUrl();
		if (url != null && !url.isEmpty()) {
			tbUser.setHeadimage(url);
		}
		return userService.editUserSome(id, tbUser);
	}
}
